#! /bin/bash

if ! [ -e temp ]; then
mkdir -p temp
fi

for i in *.MOV
do
        BASEFILENAME=$(basename “$i”)

        echo Processing file: $BASEFILENAME

    if ! [ -e temp/enc_$BASEFILENAME ]; then

         ffmpeg -i $BASEFILENAME -b 2000000 temp/enc_${BASEFILENAME/MOV/AVI}
    fi
done

echo Joining files in temp directory…then  removing temporary files
DATE=$(date +%F_%R)
echo $DATE


if ! [ -e $DATE.AVI ]; then
  avimerge -o $DATE.AVI -i temp/*.AVI
  rm -rf temp
fi

if [ -e $DATE.AVI ]; then
  if ! [ -e TO_BE_DELETED ]; then
    mkdir TO_BE_DELETED
  fi
  mv *.MOV TO_BE_DELETED
fi


echo FINISHED !!!
